<template>
  <div class="regulatory-page">
    <div class="return" @click="goBack">
      <Icon size="25" color="#000000" name="arrow-left" class="back" />
      <div class="title">
        <p class="text">账户安全</p>
      </div>
    </div>
    <div class="common-container" @click="goToPasswordManagement">
      <div class="left-section">
        <p>修改登录密码</p>
      </div>
      <div class="arrow">
        <Icon size="15" color="#808080" name="arrow" />
      </div>
    </div>
    <div class="common-container" @click="goToAccountConnection">
      <div class="left-section">
        <p>账号关联</p>
      </div>
      <div class="arrow">
        <Icon size="15" color="#808080" name="arrow" />
      </div>
    </div>
    <div class="common-container" @click="goToResetPassword">
      <div class="left-section">
        <p>重置密码</p>
      </div>
      <div class="arrow">
        <Icon size="15" color="#808080" name="arrow" />
      </div>
    </div>
    <div class="common-container" @click="DeleteAccount">
      <div class="left-section">
        <p>注销爱宠星球账户</p>
      </div>
      <div class="arrow">
        <Icon size="15" color="#808080" name="arrow" />
      </div>
    </div>
  </div>
</template>

<script>
import { Icon, Dialog, Toast } from "vant";
import http from '@/plugins/http';

export default {
  name: "SecurityView",
  components: { Icon },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    goToPasswordManagement() {
      this.$router.push('/PasswordView');
    },
    goToResetPassword() {
      Dialog.confirm({
        title: '重置密码',
        message: '你确定要向管理员申请重置密码吗？',
      }).then(() => {
        // 用户点击确认
        const username = JSON.parse(sessionStorage.getItem('username'));
        http.post(`/user/resetPasswordRequest/${username}`)
            .then(res => {
              if (res.data && res.data.statusCode === '200') {
                Toast.success('重置密码申请已提交');
              } else {
                Toast.fail(res.data.message || '重置密码申请失败');
              }
            })
            .catch(error => {
              console.error('Error:', error);
              Toast.fail('重置密码申请失败，请稍后再试');
            });
      }).catch(() => {
        // 用户点击取消
      });
    },
    goToAccountConnection() {
      this.$router.push('/ConnectionView');
    },
    DeleteAccount() {
      Dialog.confirm({
        title: '确认注销',
        message: '你确定要注销账户吗？此操作不可恢复。',
      }).then(() => {
        // 用户点击确认
        const username = JSON.parse(sessionStorage.getItem('username'));
        http.post(`/user/delete/${username}`)
            .then(res => {
              if (res.data && res.data.statusCode === '200') {
                Toast.success('账户注销成功');
                sessionStorage.clear();
                setTimeout(() => {
                  this.$router.push('/');
                }, 1000);
              } else {
                Toast.fail(res.data.message || '账户注销失败');
              }
            })
            .catch(error => {
              console.error('Error:', error);
              Toast.fail('账户注销失败，请稍后再试');
            });
      }).catch(() => {
        // 用户点击取消
      });
    }
  }
}
</script>

<style scoped>
.regulatory-page {
  background-color: #F6F7F9;
  height: 100vh;
}
.return {
  background-color: #FAEAEA;
  padding: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.back {
  position: absolute;
  left: 10px;
  text-decoration: none;
}
.title {
  display: flex;
  align-items: center;
}
.text {
  margin: 0;
  color: #000000;
  font-size: 16px;
}
.common-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 15px 15px;
  border-bottom: 0.5px solid #e0e0e0;
  cursor: pointer;
}
.left-section {
  display: flex;
  align-items: center;
}
.arrow {
  display: flex;
  align-items: center;
}
</style>
